﻿using AgentSystem.AgentBaseFunctionality.Data;
using AgentSystem.AgentBaseFunctionality.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace AgentSystem.AgentBaseFunctionality
{
    /// <summary>
    /// Base class for an agent.
    /// Provides necessary methods for interacting with the environemnt.
    /// </summary>
    public class AgentContainer : IDisposable
    {

        #region Members

        /// <summary>
        /// Agent
        /// </summary>
        private IAgent agent;

        /// <summary>
        /// Agent Web Service Service Host.
        /// </summary>
        private WebServiceHost agentWebServiceHost;

        /// <summary>
        /// Agent Service Base URL.
        /// </summary>
        public Uri AgentServiceBaseUrl { get; private set; }

        #endregion

        /// <summary>
        /// Constructor for Base Agent.
        /// This constructor will be called for any agent.
        /// </summary>
        public AgentContainer(IAgent agent ,string baseUri)
        {
            this.agent = agent;
            this.AgentServiceBaseUrl = new Uri(baseUri);
            Initialize();
        }

        /// <summary>
        /// Initialize Agent.
        /// </summary>
        private void Initialize()
        {
            ActiveAgent.SetActiveAgent(agent);
            agentWebServiceHost = new WebServiceHost(typeof(AgentService), AgentServiceBaseUrl);
            ServiceEndpoint ep = agentWebServiceHost.AddServiceEndpoint(typeof(IAgentService), new WebHttpBinding(), "");
        }

        /// <summary>
        /// Starts the Web Service and the Agent.
        /// </summary>
        public void Run()
        {
            this.agentWebServiceHost.Open();
            while (true)
            {
                agent.Run(new string[] { });
            }
        }

        /// <summary>
        /// Call an HTTP REST Service.
        /// </summary>
        /// <param name="url">URL</param>
        /// <param name="body">Body of the request.</param>
        /// <returns></returns>
        public static object CallRestService(string url)
        {
            object returnedValue = null;
            HttpWebRequest request = HttpWebRequest.CreateHttp(url);
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            using(StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                returnedValue = reader.ReadToEnd();
            }
            return returnedValue;
        }

        #region Implementing IDisposable

        ~AgentContainer()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                //free memory resources
                agentWebServiceHost.Close();
            }
            //free native resources
        }

        #endregion
    }
}
